$(document).ready(function(){
	$("#btn-register").click(function(){
		var uname = $("#username").val();
		var upass = $("#password").val();
		var confm = $("#resetpw").val();
		var unick = $("#usernick").val();
		
		if(uname == null  ||  uname == ""  ||  upass == null  ||  upass == ""  ||  confm == null  || confm == ""  ||  unick == null  ||  unick == "") alert("请填写完整的字段");
		else {
			if(confm == upass) {
				document.getElementById("btn-register").disabled = true;
				if(accountStrTest(uname) && accountStrTest(upass) && idStrTest(unick)) register(uname, upass, unick);
				else {
					alert("用户名和密码应该为8到16个字符，昵称不能含有特殊符号");
			    	$("#password").prop("value", "");
			    	$("#resetpw").prop("value", "");
			        document.getElementById("btn-register").disabled = false;
				}
			}
			else {
				alert("两次输入的口令不一致喔");
				$("#password").prop("value", "");
				$("#resetpw").prop("value", "");
			}
		}
	})
});

function register(uname, upass, unick) {
	if(uname == null  ||  uname == ""  ||  upass == null  ||  upass == ""  ||  unick == null  ||  unick == "") return;
	
    userRegister(uname, upass, unick, function() {
    	alert("注册成功！欢迎光临 ～");
		location.href="main.jsp";
		
    }, function(registerState) {
    	switch (registerState) {
			case "fail_dupname":
				alert("注册失败，您使用的用户名已经存在");
				$("#username").prop("value", "");
				break;
			case "fail_dupnick":
				alert("注册失败，您使用的昵称已经存在");
				$("#usernick").prop("value", "");
				break;
			case "fail_refusedGet":
				alert("注册失败，不允许使用GET请求注册");
				break;
			case "fail_unknow":
				alert("注册失败，出现未知异常");
				break;
    	}
    	$("#password").prop("value", "");
		$("#resetpw").prop("value", "");
		document.getElementById("btn-register").disabled = false;
    });
}